package user

import (
	"context"

	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/backend/api/internal/utils"
	"mall/service/backend/common/i18n"
	"mall/service/backend/common/utils/pointy"
	"mall/service/backend/rpc/backendclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserByIdLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetUserByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserByIdLogic {
	return &GetUserByIdLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserByIdLogic) GetUserById(req *types.IDReq) (resp *types.SystemUserInfoResp, err error) {
	data, err := l.svcCtx.BackendRpc.GetUserById(l.ctx, &backendclient.UserIdReq{
		Id:            req.Id,
		WithRoles:     pointy.GetPointer(uint32(1)),
		WithPositions: pointy.GetPointer(uint32(1)),
	})
	if err != nil {
		return nil, err
	}

	option := utils.GetOptionByStatusAndRoleIds(data.Status, data.RoleIds)

	return &types.SystemUserInfoResp{
		BaseDataInfo: types.BaseDataInfo{
			Success: true,
			Msg:     l.svcCtx.Trans.Trans(l.ctx, i18n.Success),
		},
		Data: &types.SystemUserInfo{
			BaseIDInfo: types.BaseIDInfo{
				Id:         data.Id,
				CreatedAt:  data.CreatedAt,
				UpdatedAt:  data.UpdatedAt,
				DeletedAt:  data.DeletedAt,
				CanAccept:  option.CanAccept,
				CanReject:  option.CanReject,
				CanRemove:  option.CanRemove,
				CanRestore: option.CanRestore,
			},
			Username:     data.Username,
			Nickname:     data.Nickname,
			Mobile:       data.Mobile,
			Email:        data.Email,
			Avatar:       data.Avatar,
			Status:       data.Status,
			Description:  data.Description,
			HomePath:     data.HomePath,
			DepartmentId: data.DepartmentId,
			RoleIds:      data.RoleIds,
			PositionIds:  data.PositionIds,
		},
	}, nil
}
